package com.humanres.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.GlideUtil;
import com.ciko.code.base.BaseActicity;
import com.ciko.code.bean.User;
import com.ciko.code.http.BaseResponse;
import com.ciko.code.http.Req.AddOverTimeFlowReq;
import com.ciko.code.http.Req.GetNextPersonReq;
import com.ciko.code.type.EmployeeEnum;
import com.ciko.code.utils.Util;
import com.ciko.code.utils.Utils;
import com.mywork.R;
import com.ui.pickerview.view.TimePickerView;
import com.ui.pickerview.view.TimePickerView.OnTimeSelectListener;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;


/**
 * Created by 12154 on 2015/11/10.
 */
public class ResOverWorkActivity
        extends
        BaseActicity implements OnTimeSelectListener {

    private RelativeLayout rectchooseType;
    private RelativeLayout rectchooseTimeStart;
    private RelativeLayout rectchooseTimeEnd;

    private TextView tv_startTimeLeave;
    private TextView tv_endTimeLeave;
    private TextView tv_dayCountLeave;
    private TextView tv_Retroactive;
    private TextView tv_Job;
    private TextView tv_Name;
    private ImageView ivHanderIcon;

    private EditText etresonLeave;

    private Button btnsubmitLeave;

    private long startTime = 0;
    private long endTime = 0;
    private double dayCount = 0;

    private String tye;
    private String reson = "";
    private TextView tvTypeOverWork;
    User nextUser = null;

    //时间选择器
    private TimePickerView pvTime;
    private boolean isStartTime = true;
    private SimpleDateFormat sdf = null;

    @Override
    protected void initTopBar() {
        setTitle("加班申请");
        enableNavLeftImg();
    }

    @Override
    protected void initLayout() {
        setContentView(R.layout.activity_res_over_work);
    }

    @Override
    protected void initView() {
        btnsubmitLeave = (Button) findViewById(R.id.btn_submitLeave);
        tv_startTimeLeave = (TextView) findViewById(R.id.tv_startTimeLeave);
        tv_endTimeLeave = (TextView) findViewById(R.id.tv_endTimeLeave);
        tv_dayCountLeave = (TextView) findViewById(R.id.tv_dayCountLeave);
        etresonLeave = (EditText) findViewById(R.id.et_resonLeave);
        tvTypeOverWork = (TextView) findViewById(R.id.tvTypeOverWork);
        rectchooseTimeEnd = (RelativeLayout) findViewById(R.id.rect_chooseTimeEnd);
        rectchooseTimeStart = (RelativeLayout) findViewById(R.id.rect_chooseTimeStart);
        rectchooseType = (RelativeLayout) findViewById(R.id.rect_chooseType);
        //        tv_Retroactive = (TextView) findViewById(R.id.tv_Retroactive);
        ivHanderIcon = (ImageView) findViewById(R.id.ivHanderIcon);
        tv_Job = (TextView) findViewById(R.id.tvJob);
        tv_Name = (TextView) findViewById(R.id.tvName);

        //时间选择器初始化及配置
        pvTime = new TimePickerView(this, TimePickerView.Type.YEAR_MONTH_DAY_HOUR);
        Calendar calendar = Calendar.getInstance();
        pvTime.setRange(calendar.get(Calendar.YEAR) - 10,
                calendar.get(Calendar.YEAR) + 25);
        pvTime.setTime(null);
        pvTime.setCyclic(false);
        pvTime.setCancelable(true);
        sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时");
    }

    @Override
    protected void initAdapter() {

    }

    @Override
    protected void initDates() {
        //获取审批人
        doRequestHttp(new GetNextPersonReq());
    }

    @Override
    protected void initListener() {
        rectchooseTimeStart.setOnClickListener(this);
        rectchooseTimeEnd.setOnClickListener(this);
        pvTime.setOnTimeSelectListener(this);
        btnsubmitLeave.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);

        if (v == btnsubmitLeave) {
            if (check()) {
                progressDialog.show();
                doRequestHttp(new AddOverTimeFlowReq(tye, startTime, endTime, dayCount, reson, "", nextUser.getId(), nextUser.getStaffName()));
            }
        } else if (v == rectchooseTimeEnd) {
            Utils.hideInputMethodManager(ResOverWorkActivity.this);
            isStartTime = false;
            pvTime.setTime(null);
            pvTime.show();
        } else if (v == rectchooseTimeStart) {
            Utils.hideInputMethodManager(ResOverWorkActivity.this);
            isStartTime = true;
            pvTime.setTime(null);
            pvTime.show();
        } else if (v == rectchooseType) {

        }
    }
    private void hideSoftInput() {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);
    }

    private boolean check() {

        tye = tvTypeOverWork.getText().toString().trim();
        reson = etresonLeave.getText().toString().trim();

        if (TextUtils.isEmpty(tye)) {
            showToast("请选择加班类型");
            return false;
        } else if (startTime == 0) {
            showToast("请选择开始时间");
            return false;
        } else if (endTime == 0) {
            showToast("请选择结束时间");
            return false;
        } else if (dayCount <= 0) {
            showToast("结束时间必须大于开始时间");
            return false;
        } else if (TextUtils.isEmpty(reson)) {
            showToast("请填写加班事由");
            return false;
        } else if (reson.length() > 50) {
            showToast("字数不能超过50字");
            return false;
        } else if (null == nextUser) {
            showToast("请选择审批人");
            return false;
        }

        return true;
    }

    @Override
    public void onResponseSuccess(BaseResponse<?> response, String tag) {
        super.onResponseSuccess(response, tag);
        switch (tag) {
            case "AddOverTimeFlowReq":
                progressDialog.dismiss();
                showToast("提交成功");
                Intent intent = new Intent();
                setResult(1001, intent);
                finish();
                break;
            case "GetNextPersonReq":
                nextUser = (User) response.getReturnObject();
                if (null != nextUser) {
                    tv_Job.setText(EmployeeEnum.typeOfValue(nextUser.getUserPositionMark()));
                    tv_Name.setText(nextUser.getStaffName());
                    if (!TextUtils.isEmpty(nextUser.getUserHead()))
                    GlideUtil.roundnessload(ResOverWorkActivity.this, nextUser.getUserHead(), R.drawable.test_header, ivHanderIcon);
                    if (nextUser.getUserPositionMark() == 9999) {
                        Util.showToast(this, R.string.params_less_tip);
                    }
                }
                break;
        }
    }

    private void dipatchTime(long startTime, long endTime) {
        if (endTime != 0 && startTime != 0) {
            long mss = endTime - startTime;
            int hour = (int) (mss / (60 * 60 * 1000));
            int minutes = (int) (mss % (60 * 60 * 1000));

            dayCount = (hour + (minutes == 0 ? 0 : 1));
            System.out.println("hour:" + dayCount);
            tv_dayCountLeave.setText(Util.getDayAndHour((int) dayCount));
        }
    }

    @Override
    public void onResponseFail(String tag, int resultCode) {
        super.onResponseFail(tag, resultCode);
        switch (tag) {
            case "AddOverTimeFlowReq":
                progressDialog.dismiss();
                Util.showToast(this, "提交失败，请稍后再试");
                break;
        }
    }

    @Override
    public void onTimeSelect(String date) {
        if (isStartTime) {
            try {
                if (0 == endTime) {
                    startTime = sdf.parse(date).getTime();
                    tv_startTimeLeave.setText(date);
                    tv_startTimeLeave.setTextColor(Color.BLACK);
                } else {
                    long startTempTime = sdf.parse(date).getTime();
                    if (endTime - startTempTime >= 0) {
                        startTime = startTempTime;
                        tv_startTimeLeave.setText(date);
                        tv_startTimeLeave.setTextColor(Color.BLACK);
                        dipatchTime(startTime, endTime);
                    } else {
                        Toast.makeText(ResOverWorkActivity.this, "结束时间要晚于开始时间", Toast.LENGTH_SHORT).show();
                    }
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
        } else {
            try {
                if (0 == startTime) {
                    endTime = sdf.parse(date).getTime();
                    tv_endTimeLeave.setText(date);
                    tv_endTimeLeave.setTextColor(Color.BLACK);
                } else {
                    long endTempTime = sdf.parse(date).getTime();
                    if (endTempTime - startTime >= 0) {
                        endTime = endTempTime;
                        tv_endTimeLeave.setText(date);
                        tv_endTimeLeave.setTextColor(Color.BLACK);
                        dipatchTime(startTime, endTime);
                    } else {
                        Toast.makeText(ResOverWorkActivity.this, "结束时间要晚于开始时间", Toast.LENGTH_SHORT).show();
                    }
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onBackPressed() {
        if (pvTime.isShowing()) {
            pvTime.dismiss();
        } else {
            ResOverWorkActivity.this.finish();
        }
    }
}
